<?
$sub_menu = "400200";
include_once("./_common.php");

if($_GET['w'] == "d")
{
	$w = "d";
}
else
{
	$w = $_POST['w'] ? $_POST['w'] : "";
}

$c_code = $_POST['c_code'] ? $_POST['c_code'] : "";
$c_pcode = $_POST['c_pcode'] ? $_POST['c_pcode'] : "";
$gubun = $_POST['gubun'] ? $_POST['gubun'] : "";

if(empty($w))
{
	auth_check($auth[$sub_menu], "w");

	if($gubun != "Two")
	{
		if($c_pcode=="")
		{
			$c_pcode = $c_code;
			$c_depth = "0";
		}
		else
		{
			$c_depth = $c_depth + 1;
		}

		$SQL = "INSERT INTO " . $g4['category_table'] . "
							SET c_code   = '" . $c_code . "',
									c_pcode  = '" . $c_pcode . "',
									c_depth  = '" . $c_depth . "',
									c_name   = '" . $c_name . "',
									c_sort   = '" . $c_sort . "',
									c_show   = '1',
									c_regday = now() ";

		$RESULT = sql_query($SQL);
	}
	else
	{
		$c_depth = 1;
		$c_code = "C".time();
		for($i=0;$i<count($c_name);$i++)
		{
			if($c_name[$i]!="")
			{
				if($i<10)
				{
					$code = $c_code."0".$i;
				}
				else
				{
					$code = $c_code.$i;
				}

				$SQL = "INSERT INTO " . $g4['category_table'] . "
									SET c_code   = '" . $code . "',
											c_pcode  = '" . $c_pcode . "',
											c_depth  = '" . $c_depth . "',
											c_name   = '" . $c_name[$i] . "',
											c_sort   = '" . $c_sort . "',
											c_show   = '1',
											c_regday = now() ";

				$RESULT = sql_query($SQL);

			}
		}
	}
}
else if($w == "u")
{
	auth_check($auth[$sub_menu], "w");
	$QUERY = "update g4_category set c_name='$c_name', c_sort='$c_sort', c_show   = '" . $c_show . "' where idx='$IDX' limit 1";
	$RESULT = sql_query($QUERY);
}
else if($w == "d")
{
	$w			= $_GET['w'] ? $_GET['w'] : "";
	$c_code = $_GET['c_code'] ? $_GET['c_code'] : "";

	auth_check($auth[$sub_menu], "d");
	$SQL = "delete from g4_category where c_pcode='$c_code' or c_code='$c_code'";
	$RESULT = sql_query($SQL);
}
else
{

}

goto_url("./category_list.php");
?>